﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Mvc;
using XinYi.CrookedShow.BLL;
using XinYi.CrookedShowBack.Models;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShowBack.Controllers
{
    public class MemberController : Controller
    {
        MemberService memberService = new MemberService();
        #region 会员管理-查询
        public JsonResult GetListMemberInfo(int page, int limit,string username, string start, string end)
        {
            int count = 0;
            Expression<Func<User, bool>> Wherelambda = a => true;
            DateTime startdateTime = new DateTime();
            DateTime enddateTime = new DateTime();
            //时间转string
            if (!string.IsNullOrEmpty(start))
            {
                startdateTime = Convert.ToDateTime(start);
            }
            if (!string.IsNullOrEmpty(end))
            {
                enddateTime = Convert.ToDateTime(end);
            }
            //三个条件不为空
            if (!string.IsNullOrEmpty(username)&& !string.IsNullOrEmpty(start)&& !string.IsNullOrEmpty(end))
            {
                Wherelambda = a => a.UserName.Contains(username)&&a.CreateTime>=startdateTime && a.CreateTime<=enddateTime;
            }
            //用户名、开始时间不为空
            else if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(start))
            {
                Wherelambda = a => a.UserName.Contains(username) && a.CreateTime >= startdateTime;
            }
            //用户名、至时间段不为空
            else if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(end))
            {
                Wherelambda = a => a.UserName.Contains(username) && a.CreateTime <= startdateTime;
            }
            //用户名不为空
            else if (!string.IsNullOrEmpty(username))
            {
                Wherelambda = a => a.UserName.Contains(username);
            }
            //至时间段不为空
            else if (!string.IsNullOrEmpty(end))
            {
                Wherelambda = a =>a.CreateTime >= enddateTime;
            }
            //开始时间不为空
            else if (!string.IsNullOrEmpty(start))
            {
                Wherelambda = a => a.CreateTime >= startdateTime;
            }
            Expression<Func<User, int>> OrderBylambda = a => a.Id;
            PageListResult<User> pageListResult = new PageListResult<User>();
            List<User> list = memberService.QueryPageList(page, limit, Wherelambda, OrderBylambda, out count);
            pageListResult.count = count;
            pageListResult.msg = string.Empty;
            pageListResult.code = 0;
            pageListResult.data = list.Skip((page - 1) * 10).Take(limit).ToList();
            return Json(pageListResult, JsonRequestBehavior.AllowGet);  
        }
        #endregion

        #region 会员管理-删除
        public JsonResult DelMemberInfo(User user)
        {
            Operate operate = new Operate();
            operate.Success = memberService.DeleteUser(user);
            return Json(operate);
        }
        #endregion

        #region 会员管理-查询一个数据
        public JsonResult SelectUserInfo(int Id)
        {
            User user = new User();
            Expression<Func<User, bool>> lambdaExpression = a => a.Id == Id;
            user = memberService.Query(lambdaExpression);
            return Json(user);
        }
        #endregion

        #region 会员管理-状态修改
        public JsonResult UpdateBtn(User user)
        {
            user.UpdateTime = DateTime.Now;
            Operate operate = new Operate();
            operate.Success = memberService.Update(user);
            return Json(operate);
        }
        #endregion
        
    }
}
